﻿using System;
using System.Reactive;
using Avalonia.Controls;
using chen_bot_ui.Views;
using ReactiveUI;

namespace chen_bot_ui.ViewModels
{
    public sealed class FileSelectViewModel : ViewModelBase
    {
        private string _filePath = string.Empty;

        public string FilePath
        {
            get => _filePath;
            private set => this.RaiseAndSetIfChanged(ref _filePath, value);
        }

        public IObservable<bool> IsFileSelected => this.WhenAnyValue(
            x => x.FilePath,
            x => !string.IsNullOrEmpty(x));

        public ReactiveCommand<Unit, Unit> FileSelectCommand { get; }

        public FileSelectViewModel()
        {
            FileSelectCommand = ReactiveCommand.CreateFromTask(async () =>
            {
                var dialog = new OpenFileDialog()
                {
                    AllowMultiple = false,
                    Title = "Select a file",
                    Directory = Environment.CurrentDirectory,
                };
                var result = await dialog.ShowAsync(MainWindow.Instant);
                if (result != null)
                {
                    FilePath = result[0];
                }
            });
        }
    }
}
